package com.android.infantschool.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.ClockingClockingNotBean;
import com.android.infantschool.bean.ClockingClockingYesBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.ClockClockPresenter;
import com.android.infantschool.model.mvp.presenter.ClockClockPresenterImpl;
import com.android.infantschool.model.mvp.view.ClockClockView;
import com.android.infantschool.ui.adapter.ClockingClockingAdapter;
import com.android.infantschool.util.DateUtil;
import com.android.infantschool.util.ToastUtil;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\3 0003 17:21
 * @class describe 选项卡显示考勤模块
 */
public class ClockingClockingFragment extends BaseFragment implements View.OnClickListener, ClockingClockingAdapter.OnLister, ClockClockView {

    private Button btnYesSign;
    private Button btnNotSign;
    private TextView tvSelectTime;
    private TextView tvBabyNumber;
    private LinearLayout lyClockingNonArrival;
    private LinearLayout lyClockingYesArrival;
    private RecyclerView recyclerView;

    private ClockingClockingAdapter clockingAdapter = null;
    private ClockClockPresenter presenter = null;

    private volatile boolean isShowNot = true;//true:显示未到栏目,false:显示已到栏目

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_clocking_clocking, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        presenter = new ClockClockPresenterImpl(this);
        btnYesSign = (Button) view.findViewById(R.id.btn_yes_sign);
        btnYesSign.setOnClickListener(this);
        btnNotSign = (Button) view.findViewById(R.id.btn_not_sign);
        btnNotSign.setOnClickListener(this);
        tvSelectTime = (TextView) view.findViewById(R.id.tv_select_time);
        tvSelectTime.setOnClickListener(this);
        tvBabyNumber = (TextView) view.findViewById(R.id.tv_baby_number);
        tvBabyNumber.setOnClickListener(this);
        lyClockingNonArrival = (LinearLayout) view.findViewById(R.id.ly_clocking_non_arrival);
        lyClockingYesArrival = (LinearLayout) view.findViewById(R.id.ly_clocking_yes_arrival);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        tvSelectTime.setText(DateUtil.getDate(DateUtil.ymd));
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.addItemDecoration(new DividerItemDecoration(activity, DividerItemDecoration.VERTICAL));
        clockingAdapter = new ClockingClockingAdapter(activity, this);
        recyclerView.setAdapter(clockingAdapter);
        lyClockingYesArrival.setVisibility(View.GONE);
        lyClockingNonArrival.setVisibility(View.VISIBLE);
        showYesSign();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume...");
    }

    /**
     * 请求接口数据
     *
     * @param type 0:未到接口
     * @param day  请求某一天的数据
     */
    private void getClockData(int type, String day) {
        showLoading();
        isShowNot = type == 2 ? true : false;
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("type", String.valueOf(type));
        map.put("day", day);
        Log.e(TAG, "map:" + map.toString());
        presenter.getClockClock(map, type);
    }

    @Override
    public void lister(int position, int type) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_yes_sign:
                showYesSign();
                break;
            case R.id.btn_not_sign:
                showNotSign();
                break;
            case R.id.tv_select_time:
                showYearMonthPicker();
                break;
        }
    }

    /**
     * 年月日选择
     */
    private void showYearMonthPicker() {
        new TimePickerBuilder(activity, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.ymd);
                String dateString = formatter.format(date);
                tvSelectTime.setText(dateString);
                int index = isShowNot == true ? 2 : 1;
                getClockData(index, tvSelectTime.getText().toString());
            }
        }).setSubmitColor(0xFF007C70).setCancelColor(0xFF999999).build().show();
    }

    /**
     * 未到栏目
     */
    private void showNotSign() {
        btnYesSign.setBackgroundResource(R.drawable.btn_white_right);
        btnNotSign.setBackgroundResource(R.drawable.btn_green_left);
        lyClockingYesArrival.setVisibility(View.GONE);
        lyClockingNonArrival.setVisibility(View.VISIBLE);
        btnNotSign.setTextColor(0xFFFFFFFF);
        btnYesSign.setTextColor(0xFF007C70);
        getClockData(2, tvSelectTime.getText().toString());
    }

    /**
     * 已到栏目
     */
    private void showYesSign() {
        btnYesSign.setBackgroundResource(R.drawable.btn_green_right);
        btnNotSign.setBackgroundResource(R.drawable.btn_white_left);
        lyClockingYesArrival.setVisibility(View.VISIBLE);
        lyClockingNonArrival.setVisibility(View.GONE);
        btnYesSign.setTextColor(0xFFFFFFFF);
        btnNotSign.setTextColor(0xFF007C70);
        getClockData(1, tvSelectTime.getText().toString());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isShowNot = true;
        if (presenter != null) {
            presenter.onDestroy();
            presenter = null;
        }
    }

    @Override
    public void errorClockClock(String error, int type) {
        this.dismissLoading();
        if (type == 2) {
            //todo 未到
            clockingAdapter.clear();
            tvBabyNumber.setText("未到宝宝人数(0人)");
        }
        if (type == 1) {
            //todo 已到
            clockingAdapter.clear();
            tvBabyNumber.setText("已到宝宝人数(0人)");
        }
    }

    @Override
    public void successClockClock(BaseBean baseBean, int type) {
        this.dismissLoading();
        if (type == 2) {
            ClockingClockingNotBean notBean = (ClockingClockingNotBean) baseBean;
            if (notBean == null) {
                tvBabyNumber.setText("未到宝宝人数(0人)");
                ToastUtil.showShortToast(activity, "网络异常");
                return;
            }
            if (notBean.getCode() == 0) {
                recyclerView.setAdapter(clockingAdapter);
                clockingAdapter.setNotData(notBean.getData());
                tvBabyNumber.setText("未到宝宝人数(" + notBean.getData().size() + "人)");
            } else {
                tvBabyNumber.setText("未到宝宝人数(0人)");
                clockingAdapter.clear();
                ToastUtil.showShortToast(activity, "获取失败:" + notBean.getMsg());
            }
        }
        if (type == 1) {
            ClockingClockingYesBean yesBean = (ClockingClockingYesBean) baseBean;
            if (yesBean == null) {
                tvBabyNumber.setText("已到宝宝人数(0人)");
                ToastUtil.showShortToast(activity, "网络异常");
                return;
            }
            if (yesBean.getCode() == 0) {
                recyclerView.setAdapter(clockingAdapter);
                clockingAdapter.setYesData(yesBean.getData());
                tvBabyNumber.setText("已到宝宝人数(" + yesBean.getData().size() + "人)");
            } else {
                tvBabyNumber.setText("已到宝宝人数(0人)");
                clockingAdapter.clear();
                ToastUtil.showShortToast(activity, "获取失败:" + yesBean.getMsg());
            }
        }
    }
}
